$().ready(function(){
	getSessionId();///获取sessionid
	vew_page();
	init();
	setJoinAtOnce();
	setActFocus();
	setMemberList();
	loadComment();
	rightLoadMember();
	userReputation();
	userVote();
	$("#activitymenus>ul>li").eq(0).click(function(){
			$("#activitymenus").find('.current').removeClass('current');
			$('.menu-list').hide();
			$('#activitycontent').show();
			$(this).addClass('current')
		return false;
	});

	//setUploadInt();
	//listActPic();
})
function init(){
	$.ajax({type:"POST",
			dataType:"json",
				data:{
						"dataFromType":"ajax",
						"id":($("#artcile_id").val())
					},
				url:($("#newsurl").val())+"e/jat/act_info.php?tmp="+Math.random(),
		success:function(r){
			if(r.flag==1){									
				$('.join_num').html(r.data.join_num);
				$('.focus_num').html(r.data.focus_num);
				$('.comment_num').html(r.data.comment_num);
				$('.checked_num').html(r.data.onclick);
				var m={0:"进行中",4:"已结束"};
				var index=r.data.status;
				$(".focus_act").attr("able",1);
				if(r.focused==1){
					$(".focus_act").attr("status",1);						
					$(".focus_act").html("已经关注");
					$(".focus_act").removeClass("act_focus_can");
					$(".focus_act").addClass("act_focus_ed");
				}else{
					
				}
				if(r.reged==-1){
					$(".join_act").show();
					$(".join_act").attr("able",1)
					//$(".join_act").html("已经报名");				
				}else if(r.reged==1){
					$(".join_act").attr("able",0);
					$(".join_act").html("已报名等待批准");
				}else if(r.reged==2){
					$(".join_act").attr("able",0);
					$(".join_act").attr("color","red");
					$(".join_act").html("您被拒绝参加");
				}else if(r.reged==11){
					$(".join_act").attr("able",0);
					$(".join_act").css("color","green");
					$(".join_act").html("已被批准");
				}else{
					$(".join_act").attr("able",0);
				}
				$('.status').html(m[index]);
				return false;
			}else if(r.msg){
				alert(r.msg);	
			}else{
				alert("系统错误，请与管理员联系");
			}
				
	}});
		$(".comments_text>textarea").val("").attr("disabled",'');
	$(".submitbtn").click(function(){//提交内容
	if(window.uploading==true){
		alert('请勿重复提交！');
		return false;
	}
		var content=$(".comments_text>textarea").val();
		if(!content){alert("内容不能为空！");return false;}
		if(content.length<2){alert("内容不能小于两个字!");return false;}
		if(content.length>200){
			if(!confirm('字符超过限制，系统将自动截取200个字，是否继续？')){
				return false;
			}
			content=content.subString(0,200);
		}
		$(".comments_text>textarea").attr("disabled",'disabled');
		window.uploading=true;
		$.ajax({type:"POST",dataType:"json",data:{"dataFromType":"ajax","act":"add_comment","content":content,"act_id":($("#artcile_id").val())},url:($("#newsurl").val())+"e/comment/",success:function(r){
			if(r.flag==1){
				$(".comments_text>textarea").val("").attr("disabled",'');
				window.uploading=false;
				loadComment();					
				return false;
			}else if(r.msg){alert(r.msg);}else{alert("系统错误，请与管理员联系");}
				
	}});	
	});
}
function userReputation(){
	$(".user_reputation").click(function(){
			$("#activitymenus").find('.current').removeClass('current');
			$('.menu-list').hide();
			$('#activitycontent').hide();
			$(this).addClass('current')
			$(".vote_reputation").show().html('加载中...').load(($("#newsurl").val())+"e/module/list_reputation.php?id="+($("#artcile_id").val())+"&tmp="+parseInt(Math.random()*10000),function(){
				
			});
			return false;
	});
}
function userVote(){
	$(".act_fen>a").click(function(){
			$("#activitymenus").find('.current').removeClass('current');
			$('.menu-list').hide();
			$('#activitycontent').hide();
			$(this).parent().addClass('current')
		$(".vote_list").show().load(($("#newsurl").val())+"e/module/list_act_vote.php?id="+($("#artcile_id").val())+"&tmp="+parseInt(Math.random()*10000),function(){
			$(".vote-button").click(function(){
				if(window.isAddingVote){return false;}
				var v1	=$("input[name=vote1]:checked").val();
				var v2	=$("input[name=vote2]:checked").val();
				var v3	=$("input[name=vote3]:checked").val();
				var v4	=$("input[name=vote4]:checked").val();
				if(!(v1 && v2 && v3 && v4)){
					alert('请填写完整全；');
					return false;
				}
				window.isAddingVote=true;
				$.ajax({
						type:"POST",
						dataType:"json",
						data:{
							"dataFromType":"ajax",
							"act":"add_vote",
							"id":($("#artcile_id").val()),
							'v1':v1,
							'v2':v2,
							'v3':v3,
							'v4':v4
						},
						url:($("#newsurl").val())+"e/jat/",
						success:function(r){
						if(r.flag==1){
							window.isAddingVote=false;
							$(".act_fen>a").click();
						}else{
							alert(r.msg);
						}
					
					}});
			});//
		});
		return false;
	})
}
function loadComment(p){
	var height=$(".list-c-items").height();
	$(".list-c-items").html('加载中...');
	//$(".list-c-items").css("height",height);
	var url=($("#newsurl").val())+"e/comment/list.php?p="+(p?p:1)+"&act_id="+($("#artcile_id").val())+"&tmp="+parseInt(Math.random()*10000);
	$(".list-c-items").load(url,function(){
		//$(".list-c-items").css("height","auto");
		var cmNum=$("#cmmt_t_num").val();
		$(".comment_num").html(cmNum);
	})
}
/**
**设置标签
**
**/
function vew_page(){
		$.ajax({type:"POST",
			dataType:"json",
				data:{
						"dataFromType":"ajax",
						"id":($("#artcile_id").val())
					},
				url:($("#newsurl").val())+"e/jat/page_view.php",
		success:function(r){
			if(r.flag==1){									
				
				return false;
			}else if(r.msg){
				alert(r.msg);	
			}else{
				alert("系统错误，请与管理员联系");
			}
				
	}});
}
function onLineSayMsg(text){
	alert(text);
}
function setJoinAtOnce(){
	$(".join_act").click(function(){
		if($(this).attr("able")!=1){return false;}
		popUpLoadUrl("我要报名",'join_act',{"act":"join_act"},function(){
				$(".join_submit").click(function(){
					//var frm=$(".join_act").serialize();
						var val1=$(".join_actbox").find("input").serialize();	
						var val2=$(".join_actbox").find("textarea").serialize();	
						var val3=$(".join_actbox").find("select").serialize();	
						vals=val1+"&"+val2+"&"+val3;						
					var truename=$(".truename").val()
						if(!truename){
							alert("姓名不能为空！");
							$(".truename").trigger("focus");
							ipt_error($(".truename"));
							return false;
						}else if(truename.length<2){
							alert("姓名长度必须是2位或2位以上！");
							$(".truename").trigger("focus");
							ipt_error($(".truename"));
							return false;
						}
					var val=$(".corporation").val()
						if(!val){
							alert("学校或单位不能为空！");
							$(".corporation").trigger("focus");
							ipt_error($(".corporation"));
							return false;
						}else if(val.length<6){
							alert("学校或单位长度必须6位或6位以上！");
							$(".corporation").trigger("focus");
							ipt_error($(".corporation"));
							return false;
						}
					var val=$(".tel").val()
						if(!val){
							alert("联系方式不能为空！");
							$(".tel").trigger("focus");
							ipt_error($(".tel"));
							return false;
						}else if(val.length<7){
							alert("联系方式长度必须在7位以上！");
							$(".tel").trigger("focus");
							ipt_error($(".tel"));
							return false;
						}
						var data=vals+"&dataFromType=ajax&act_id="+($("#artcile_id").val());
						$.ajax({type:"POST",
							dataType:"json",
								data:data,
								url:($("#newsurl").val())+"e/jat/join_act.php",
						success:function(r){
							if(r.flag==1){
								$(".join_act").attr("able",0);
								$(".join_act").html("已报名等待批准");
								showPopupMsg('报名成功，请等待活动建立者批准！');
							}else if(r.msg){
								alert(r.msg);	
							}else{
								alert("系统错误，请与管理员联系");
							}
								
					}});
					/**/
				});			


			});
	});
}
function setMemberList(){
	$('.act_mermber_list').click(function(){loadThePageM(1);return false;});
}
function loadThePageM(pageNum){
	popUpLoadUrl("报名列表",'member_list',{"act":'member_list',"&act_id":($("#artcile_id").val()),"&page":(pageNum?pageNum:1),"&tmp":(Math.random())},function(){setMyActOpt();});
			return;
	
}
function setMyActOpt(){
	$(".act-allow-btn").click(function(){
		var id=$(this).attr("act_id");
		var flag=11;
		setActStatus(id,flag);
	})
	$(".act-deny-btn").click(function(){
		var id=$(this).attr("act_id");
		var flag=2;
		setActStatus(id,flag);
	})
}
function setActStatus(id,flag){
$.ajax({type:"POST",
				dataType:"json",
					data:{
							"dataFromType":"ajax",
							"act":"set_reg_status",
							"status":(flag),
							"reg_id":(id)
						},
					url:($("#newsurl").val())+"e/jat/",
			success:function(r){
				if(r.flag==1){
						var m={"1":"act-wait","2":"act-deny","11":"act-allow"};
						$(".act_reg_"+id).find(".act-status").removeClass("act-wait").removeClass("act-deny").removeClass("act-allow").addClass(m[flag]);
					return false;
				}else if(r.msg){
					alert(r.msg);	
				}else{
					alert("系统错误，请与管理员联系");
				}
					
		}});
}
function setActFocus(){
	$(".focus_act").click(function(){
		if($(this).attr("able")!=1){alert('初始化未完成，请等待！');return false;}
		var act_id=$(this).attr("act_id");
		var status=$(this).attr("status");
		var _this=this;
			$.ajax({type:"POST",
				dataType:"json",
					data:{
							"dataFromType":"ajax",
							"act":"act_focus",
							"status":(status==1?0:1),
							"act_id":($("#artcile_id").val())
						},
					url:($("#newsurl").val())+"e/jat/",
			success:function(r){
				if(r.flag==1){						

						$(_this).attr("status",(status==1?0:1));
						if(status==0){
							$(_this).removeClass("act_focus_can");
							$(_this).addClass("act_focus_ed");
							$(_this).html("已经关注");
						}else{
							$(_this).removeClass("act_focus_ed");
							$(_this).addClass("act_focus_can");
							$(_this).html("关注活动");
						}
					return false;
				}else if(r.msg){
					alert(r.msg);	
				}else{
					alert("系统错误，请与管理员联系");
				}
					
		}});
	});
}
function rightLoadMember(){
	$(".parts").html("加载中...");
	$(".parts").load(($("#newsurl").val())+"e/jat/right_m_list.php?act_id="+($("#artcile_id").val()),function(){});
}
function ipt_error(T){
	$(".error_box").removeClass("error_box");
	T.addClass("error_box");	
}
function setUploadInt(){
	/*$('.act-album').click(function(){
		setuploadPic();
		return false;
	});*/
}
function setuploadPic(){
	$(".vote_upload").show();		
}
function getSessionId(){
	$.ajax({
		type:"POST",dataType:"json",data:{"dataFromType":"ajax","act":"session_id"},
			url:($("#newsurl").val())+"e/jat/",
			success:function(r){
				if(r.flag==1){
					window.session_id=r.msg;
					$("#session_id").val(window.session_id);
					initFlashUpload();
					return false;
				}else if(r.msg){
					alert(r.msg);	
				}else{
					alert("系统错误，请与管理员联系");
				}
					
		}});
}
function runAtLogin(func){
	
}
function setUpFileStart(){
	$('#file_upload').uploadifyUpload();
}
function listActPics(){
	
}
function initFlashUpload(){
$('#file_upload').uploadify({
			'uploader'       : ($("#newsurl").val())+'skin/jquery.uploadify-v2.1.4/uploadify.swf',
			'script'         : ($("#newsurl").val())+'e/jat/act_up_pic.php',
			'cancelImg'      : ($("#newsurl").val())+'skin/jquery.uploadify-v2.1.4/cancel.png',
			'folder'         : ($("#newsurl").val())+'skin/upload',
			'multi'          : true,
			'auto'           : false,
			"scriptData"	 : {"id":$("#artcile_id").val(),'sid':($("#session_id").val()||"")},
			'onComplete'  : function(event, ID, fileObj, response, data) {						
				var o=getObject(response)
			if(o){
				if(o.flag==1){
					alert(o.id);
				}else{
					alert(o.msg);
				}
			}else{
				alert('系统错误！');
			}		
		},
		"":function(){
		
		},
		"onAllComplete"	:function(){					
			//$(".list-p").append("<div class='clear'></div>");
			//$(".ok_div").show();
		}
		  });
}
function listActPic(){
	$(".list_act_pic").show().html('loading').load(($("#newsurl").val())+"e/module/list_act_pic.php?id="+$("#artcile_id").val()+"&tmp="+Math.random(),function(){
		//alert('ok');
	});
}
function editActPic(i){
	popUpLoadUrl("图片编辑",'m_edit_pic',{"act":"m_edit_pic","&id":i},function(){
		$('.edi_one_pic').find('.ok_submit').click(function(){
			var t=$('.edi_one_pic').find('.title').val();
			var c=$('.edi_one_pic').find('.content').val();
			if(!t){alert('名称不能为空！');return false;}
			if(t.length>50){alert('名称长度不合法，请填写50个字符以内字符！');return false;}
			///if(!c){alert('不能为空！');return false;}
			if(c.length>200){if(!confirm('描述字符过长，后台将自动截取20个字符，是否继续？')){return false;}}
			$.post(($("#newsurl").val())+'e/jat/',{'act':'edit_one_pic','i':i,'t':t,'c':c},function(r){
					if(r.flag==1){					
						popup_close();
						listActPic();
					}else if(r && r.msg){
						alert(r.msg);
					}else{
						alert('系统错误！');
					}
				},'json');
		});
	});
	//return false;
}
function deleteActPic(i){
	if(confirm('确认删除？')){
		$.post(($("#newsurl").val())+'e/jat/',{'act':'delete_one_pic','i':i},function(r){
					if(r.flag==1){					
						popup_close();
						listActPic();
					}else if(r && r.msg){
						alert(r.msg);
					}else{
						alert('系统错误！');
					}
		},'json')
	}
}
function show_one_pic(id){
	$(".list_act_pic").show().html('加载中...').load(($("#newsurl").val())+"e/module/m_show_one_pic.php?id="+id+"&r="+parseInt(Math.random()*10000),function(){
				
	});
}
function viewPrePic(){
	
}
function viewNextPic(){
	
}